package com.bayss.core.service.address;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.address.AddressFixedInfo;
import com.bayss.core.entity.address.AddressFixedInfoExample;
import com.bayss.core.entity.address.mapper.AddressFixedInfoMapper;
import com.bayss.core.entity.station.Station;
import com.bayss.core.entity.station.StationExample;
import com.bayss.core.entity.station.mapper.StationMapper;
import com.bayss.core.util.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by LDL on 2019/12/7.
 */
@Service
public class AddressFixedInfoService {
    @Autowired
    private AddressFixedInfoMapper addressFixedInfoMapper;
    @Autowired
    private StationMapper stationMapper;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param stationId
     * @param addrAlias
     * @param receivingAddr
     * @return
     */
    public PageInfo<AddressFixedInfo> getAddressFixedByPage(Integer page, Integer pageSize, Long stationId, String addrAlias, String receivingAddr) {
        AddressFixedInfoExample example = new AddressFixedInfoExample();
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page, pageSize);
        AddressFixedInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(addrAlias)) {
            criteria.andAddrAliasLike(addrAlias);
        }
        if (!StringUtils.isEmpty(receivingAddr)) {
            criteria.andReceivingAddrLike(receivingAddr);
        }
        if (stationId != null) {
            criteria.andStationIdEqualTo(stationId);
        }
        PageInfo<AddressFixedInfo> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> addressFixedInfoMapper.selectByExample(example));
        if (CollectionUtils.isNotEmpty(result.getList())) {
            //带上门店信息
            getStationInfo(result.getList());
        }
        return result;
    }

    /**
     * 固定地址list
     *
     * @return
     */
    public List<AddressFixedInfo> getAddressFixedList() {
        AddressFixedInfoExample example = new AddressFixedInfoExample();
        List<AddressFixedInfo> list = addressFixedInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            //带上门店信息
            getStationInfo(list);
        }
        return list;
    }

    /**
     * 获取门店信息
     *
     * @param list
     */
    private void getStationInfo(List<AddressFixedInfo> list) {
        StationExample stationExample = new StationExample();
        List<Long> stationIdList = list.stream().map(AddressFixedInfo::getStationId).collect(Collectors.toList());
        stationExample.createCriteria().andIdIn(stationIdList);
        List<Station> stations = stationMapper.selectByExample(stationExample);
        Map<Long, Station> idAndStationMap = stations.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
        for (AddressFixedInfo addressFixedInfo : list) {
            addressFixedInfo.setStation(idAndStationMap.get(addressFixedInfo.getStationId()));
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        addressFixedInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改
     *
     * @param addressFixedInfo
     */
    public void updateAddressFixedInfo(AddressFixedInfo addressFixedInfo) {
        addressFixedInfoMapper.updateByPrimaryKeySelective(addressFixedInfo);
    }

    /**
     * 新增固定地址
     *
     * @param addressFixedInfo
     * @return
     */
    public ResultMsg addAddressFixedInfo(AddressFixedInfo addressFixedInfo) {
        if (addressFixedInfo.getStationId() == null) {
            return ResultMsg.fail(-1, "请选择门店");
        }
        if (stationMapper.selectByPrimaryKey(addressFixedInfo.getStationId()) == null) {
            return ResultMsg.fail(-1, "门店不存在");
        }
        addressFixedInfo.setCreateTime(new Date());
        addressFixedInfoMapper.insertSelective(addressFixedInfo);
        return ResultMsg.success();
    }

    /**
     * 校验订单时间是否在门店截止时间范围里
     *
     * @param stationId
     * @param orderTime
     * @return
     */
    public boolean checkSendTime(long stationId, Date orderTime) {
        AddressFixedInfoExample example = new AddressFixedInfoExample();
        AddressFixedInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStationIdEqualTo(stationId);
        List<AddressFixedInfo> list = addressFixedInfoMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            //门店未配置固定地址
            return false;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(orderTime);
        String orderTimeStr = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);

        AddressFixedInfo addressFixedInfo = list.get(0);
        String[] timeArr = addressFixedInfo.getSendTime().split(",");
        for (String timeStr : timeArr) {
            String sendTime = timeStr.split("#")[0];
            String start = sendTime.split("_")[0];
            String end = sendTime.split("_")[1];
            if (orderTimeStr.compareTo(start) >= 0 && end.compareTo(orderTimeStr) <= 0) {
                return true;
            }

        }
        return false;
    }
}
